package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/permisson")
@Slf4j
public class PermissonController {
    @Reference
    private PermissionService permissionService;

    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        log.debug("controller:{"+permission+"}");
        permissionService.add(permission);
        log.debug("权限数据添加成功！！");
        return new Result(true, MessageConst.ADD_PERMISSION_SUCCESS);
    }

    /**
     * 分页查询
     *  1.调用Service获取分页结果数据
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        log.debug("controller{"+queryPageBean+"}");
        PageResult pageResult = permissionService.findByPage(queryPageBean);
        log.debug("权限数据分页查询成功!!");
        return new Result(true, MessageConst.QUERY_PERMISSON_SUCCESS, pageResult);
    }

    //删除
    @RequestMapping("/delById")
    public Result delById(Integer id){
        log.debug("controller -- delete: {"+id+"}");
        permissionService.delById(id);
        log.debug("权限数据删除成功!!");
        return new Result(true,MessageConst.DELETE_PERMISSON_SUCCESS);
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        log.debug("controller -- findById: {"+id+"}");
        Permission permission = permissionService.findById(id);
        log.debug("权限数据id查询：{"+ permission +"}");
        return  new Result(true, MessageConst.QUERY_PERMISSON_SUCCESS,permission);

    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        log.debug("controller -- edit: {"+permission+"}");
        permissionService.edit(permission);
        log.debug("权限数据成功！！！");
        return new Result(true,MessageConst.EDIT_PERMISSION_SUCCESS);
    }

      //查询所有检查项
     @RequestMapping("/findAll")
    public Result findAll(){
        log.debug("PermissionController:findAll: " );
        List<Permission> permissionList = permissionService.findAll();
        log.debug("检查组查询成功！！！");
        return new Result(true, MessageConst.QUERY_CHECKGROUP_SUCCESS,permissionList);
    }

}
